package com.lianliankan.llkan;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.example.llkan.R;
import com.lianliankan.util.ButtonSelector;
import com.lianliankan.util.ExtendMethod;
import com.lianliankan.util.RankAsyncTask;

public class RankActivity extends Activity {
	private ListView listview;
	private ImageButton beginButton;
	private ImageButton optionButton;
	private TextView liveText;
	private TextView moneyText;
	private TextView dimondText;
	private ExtendMethod extendMethod;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_rank);
		findView();
		init();
	}

	private void findView() {
		beginButton = (ImageButton) findViewById(R.id.rankBeginButton);
		optionButton = (ImageButton) findViewById(R.id.optionButton);
		listview = (ListView) findViewById(R.id.ranklistview);
		liveText = (TextView) findViewById(R.id.live);
		moneyText = (TextView) findViewById(R.id.money);
		dimondText = (TextView) findViewById(R.id.dimond);
	}

	private void init() {
		ButtonSelector bSelector = new ButtonSelector();

		beginButton.setBackgroundDrawable(bSelector.newSelector(
				RankActivity.this, R.drawable.gamebigin,
				R.drawable.gamebiginpress, R.drawable.gamebiginpress,
				R.drawable.gamebiginpress));
		optionButton.setBackgroundDrawable(bSelector.newSelector(
				RankActivity.this, R.drawable.settingbtn,
				R.drawable.settingpress, R.drawable.settingpress,
				R.drawable.settingpress));
		beginButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// extendMethod.buttonClickSound(RankActivity.this);
				Intent intent = new Intent(RankActivity.this,
						StagePropertyActivity.class);
				startActivity(intent);
				finish();
			}

		});
		optionButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(RankActivity.this,
						OptionsActivity.class);
				startActivity(intent);
			}

		});

		RankAsyncTask task = new RankAsyncTask(liveText, moneyText, dimondText,
				listview, this);
		task.execute();
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			Intent intent = new Intent(RankActivity.this, WelcomeActivity.class);
			startActivity(intent);
			finish();
		}
		return false;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.welcome, menu);
		return true;
	}

	protected void onResume() {
		if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		}
		super.onResume();
	}
}
